package com.tedu.dao.impl;

import com.tedu.dao.Test1Dao;
import com.tedu.entity.Test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//在dao目录下新建javaclass   输入impl.Test1DaoImpl 回车
public class Test1DaoImpl implements Test1Dao {
    @Override
    public List<Test1> queryAll() {
        List<Test1> list = null;
        try {
            //1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url = "jdbc:mysql://localhost:3306/ankang";
            String username = "root";
            String password = "root";
            Connection conn = DriverManager.getConnection(url, username, password);
            //3.执行sql语句
            String sql = "select * from test1";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            //4.处理查询结果
            //创建一个集合
            list = new ArrayList<>();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                //调用有参的构造
                Test1 test1 = new Test1(id,name);
                //将对象添加到集合中
                list.add(test1);
            }
            //5.释放资源
            rs.close();
            pstm.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void insertTest1(Test1 test1) {
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url = "jdbc:mysql://localhost:3306/ankang?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
            //数据库用户名 密码
            String username = "root";
            String password = "root";
            Connection conn = DriverManager.getConnection(url, username, password);
            //3.执行sql语句
            String sql = "insert into test1(name) value(?)";
            PreparedStatement pstm = conn.prepareStatement(sql);
            //给？设置值
            pstm.setString(1, test1.getName());
            //表示执行完sql影响了一行
            int num = pstm.executeUpdate();
            System.out.println("影响了 "+ num + "行");
            //4.释放资源
            pstm.close();
            conn.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateTest1(Test1 test1) {
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取链接
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ankang?characterEncoding=utf-8",
                    "root",
                    "root");
            //执行sql语句
            String sql = "update test1 set name = ? where id = ?";
            //给？设置值
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setString(1, test1.getName());
            pstm.setInt(2, test1.getId());
            int num = pstm.executeUpdate();
            System.out.println("影响了"+ num + "行");
            //释放资源
            pstm.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteTest1(int id) {
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ankang"
                    , "root", "root");
            String sql = "delete  from test1 where id = ?";
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setInt(1, id);
            int num = pstm.executeUpdate();
            System.out.println("影响了"+num+"行");
            pstm.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
